<?php

namespace App\Console\Commands;

use App\model\Coustomer;
use Carbon\Carbon;
use Illuminate\Console\Command;

class SeaCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sea';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '将半圆以前客户全部转成公海客户';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        if ($this->isTime()==true){
            Coustomer::where('created_at','<',Carbon::now()->addDays(-30))
                ->where('created_at','>',Carbon::now()->addDays(-37))
                ->whereHas('project',function ($q){
                })
                ->chunkById(100,function($coustomers){
                    foreach ($coustomers as  $item){
                        if ($item->is_sea!=1&&$item->status!=Coustomer::Completed){//不是公海的客户转移到公海
                            $customer=  Coustomer::whereMobile($item->mobile)->where('is_sea',1)->first();
                            if (!$customer) {
                                $item->is_sea = 1;
                                $item->save();
                                $this->info($item->customer);
                            }
                        }
                    }
                });
        }

    }

    /**
     * 判断是否执行
     */
    protected function isTime(){
        $days= Carbon::now()->diff(Carbon::parse('2020-05-01'))->days;
       //$days=Carbon::parse('2020-05-08')->diff(Carbon::parse('2020-05-01'))->days;
       if ($days%7==0){
           return true;
       }
       return false;
    }
}
